<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>购物车</title>
        <%--base 和 jq文件 css--%>
        <jsp:include page="/pages/common/head.jsp"></jsp:include>
        <script>
            $(function () {
                $(".delete_item").click(function () {
                    return confirm("确定删除《" + $(this).parent().parent().find("td:first").text() + "》");
                })
                $(".clear_item").click(function () {
                    return confirm("确定清空购物车吗");
                })
                $(".update_count").change(function () {
                    let count = $(this).val();
                    if (confirm("确定将《" + $(this).parent().parent().find("td:first").text() + "》的数量修改为" + count)) {
                        let id = $(this).attr("bookId");
                        location.href = "${basePath}cartServlet?action=updateCount&count=" + count + "&id=" + id;
                    } else {
                        this.value = this.defaultValue;
                    }
                })
            })
        </script>
    </head>
    <body>

        <div id="header">
            <img class="logo_img" alt="" src="static/img/logo.gif">
            <span class="wel_word">购物车</span>
            <jsp:include page="/pages/common/login_menu.jsp"></jsp:include>
        </div>

        <div id="main">

            <table>
                <tr>

                    <td>商品名称</td>
                    <td>数量</td>
                    <td>单价</td>
                    <td>金额</td>
                    <td>操作</td>
                </tr>
                <%--  购物车没有有数据实现--%>
                <c:if test="${empty sessionScope.cart.items}">
                    <td colspan="5"><a href="index.jsp">没有商品前去选购--></a></td>
                </c:if>
                <c:if test="${not empty sessionScope.cart.items}">
                    <c:forEach items="${sessionScope.cart.items}" var="entry">
                        <tr>
                            <td>${entry.value.name}</td>
                            <td>
                                <input class="update_count" type="text" bookId="${entry.value.id}"
                                       value="${entry.value.count}" style="width: 30px">
                            </td>
                            <td>${entry.value.price}</td>
                            <td>${entry.value.totalPrice}</td>
                            <td><a class="delete_item" href="cartServlet?action=deleteItem&id=${entry.value.id}">删除</a>
                            </td>
                        </tr>
                    </c:forEach>
                </c:if>


            </table>
            <%-- 购物车中没有信息则不显示	--%>
            <c:if test="${not empty sessionScope.cart.items}">
                <div class="cart_info">
                    <span class="cart_span">购物车中共有<span class="b_count">${sessionScope.cart.totalCount}</span>件商品</span>
                    <span class="cart_span">总金额<span class="b_price">${sessionScope.cart.totalPrice}</span>元</span>
                    <span class="cart_span"><a class="clear_item" href="cartServlet?action=clear">清空购物车</a></span>
                    <span class="cart_span"><a href="orderServlet?action=createOrder">去结账</a></span>
                </div>
            </c:if>


        </div>

        <%--页脚内容--%>
        <jsp:include page="/pages/common/footer.jsp"></jsp:include>
    </body>
</html>